import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import store from "@/store";
import request from "@/request";

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
    },
    {
        path: "/login",
        name: "login",
        component: () => import(/* webpackChunkName: "about" */ '@/views/login/LoginView.vue')
    },
    {
        path: "/register",
        name: "register",
        component: () => import(/* webpackChunkName: "about" */ '@/views/login/RegisterView.vue')
    },
    {
        path: "/password",
        name: "password",
        component: () => import(/* webpackChunkName: "about" */ '@/views/login/PasswordView.vue')
    },
    {
        path: "/pass/reset",
        name: "pass-reset",
        component: () => import(/* webpackChunkName: "about" */ '@/views/user/PersonPassView.vue')
    },
    {
        path: "/post",
        name: "post",
        component: () => import(/* webpackChunkName: "about" */ '@/views/post/PostView.vue'),
        children: [
            {
                path: "/post/list",
                name: "post-list",
                component: () => import(/* webpackChunkName: "about" */ '@/views/post/PostListView.vue')
            },
            {
                path: "/post/collect",
                name: "post-collect",
                component: () => import(/* webpackChunkName: "about" */ '@/views/post/PostCollectView.vue')
            },
            {
                path: "/post/look",
                name: "post-look",
                component: () => import(/* webpackChunkName: "about" */ '@/views/post/PostLookView.vue')
            },
            {
                path: "/post/detail",
                name: "post-detail",
                component: () => import('@/views/post/PostDetailView.vue'),
                props: {
                    post: {}
                }
            },
            {
                path: "/post/edit",
                name: "post-edit",
                component: () => import('@/views/post/PostEditView.vue')
            }
        ]
    },
    {
        path: "/user",
        name: "user",
        component: () => import(/* webpackChunkName: "about" */ '@/views/user/UserInfoView.vue')
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

const checkToken = async () => {
    let real = false;
    await request.get("/test/hello").then(res => {
        console.log("res");
        real = res.status === 200 && (res.data.code < 400 || res.data.code > 410);
        console.log(res);
    }, err => {
        console.log(err);
    });
    return real;
}
// 登录拦截
router.beforeEach(async (to, from, next) => {
    console.log("to:", to.path, "from:", from.path)
    let token = store.getters.getToken();
    console.log(token, !token);
    // request.get("/test/hello").then(res => {
    //     console.log(res)
    //     if (res.status === 200 && (res.data.code < 400 || res.data.code > 410)) {
    //         if (to.path === "/login") {
    //             to.path = "/";
    //             next({name: "home"});
    //         }
    //         next();
    //     } else {
    //         console.log("404", to.path);
    //         if (to.path === "/register" || to.path === "/" || to.path === "/password") {
    //             next();
    //         } else if (to.path !== "/login") {
    //             console.log("other")
    //             to.path = "/login";
    //             next({name: "login"});
    //         }
    //     }
    // }, err => {
    //     console.log(err);
    // });
    await checkToken().then(r => {
        console.log("11111111111111111");
        if (r) {
            if (to.path === "/login") {
                to.path = "/";
                next({name: "home"});
            }
            next();
        } else {
            console.log("404", to.path);
            if (to.path === "/register" || to.path === "/" || to.path === "/password") {
                next();
            } else if (to.path !== "/login") {
                console.log("other")
                to.path = "/login";
                next({name: "login"});
            }
        }
    });
    console.log("2222222222222");
    next()

    // if (token) {
    //     if (to.path === "/login") {
    //         to.path = "/";
    //         next({name: "home"});
    //         return;
    //     }
    // } else {
    //     if (to.path === "/register" || to.path === "/" || to.path === "/password") {
    //     } else if (to.path !== "/login") {
    //         to.path = "/login";
    //         next({name: "login"});
    //     }
    // }
    // next();
});

export default router
